/*
 *http://www.apache.org/licenses/LICENSE-2.0
 *Copyright (c) 2021 fullset
 *@ClassName SysOrgServiceImpl
 *@date 2021/6/29 15:50
 *@author: pangps
 *@Description TODO
 */
package com.fs.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.base.service.SysOrgService;
import com.fs.orm.dao.SysOrgDao;
import com.fs.orm.entity.SysOrg;
import com.fs.orm.layui.LayTree;
import com.fs.orm.layui.LayTreeBuilder;
import com.fs.orm.layui.TableTree;
import com.fs.orm.layui.TableTreeBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysOrgServiceImpl implements SysOrgService {

    @Autowired
    private SysOrgDao sysOrgDao;
    @Override
    public List<LayTree> getLayTree() {
        List<LayTree> list = new ArrayList<>();
        QueryWrapper qWrapper = new QueryWrapper();
        qWrapper.eq("is_deleted", 0);
        List<SysOrg> sysOrgList = sysOrgDao.getList(qWrapper);
        for(SysOrg sysOrg : sysOrgList){
            LayTree layTree = new LayTree();
            layTree.setId(String.valueOf(sysOrg.getId()));
            layTree.setTitle(sysOrg.getName());
            layTree.setParentId(String.valueOf(sysOrg.getParentId()));
            layTree.setSpread(true);
            list.add(layTree);
        }
        LayTreeBuilder lb = new LayTreeBuilder(list);
        list = lb.builTree();
        return list;
    }

    @Override
    public List<TableTree> getTableTree(QueryWrapper qWrapper) {
        List<TableTree> list = new ArrayList<>();
        List<SysOrg> listSysOrg = sysOrgDao.getList(qWrapper);
        for(SysOrg sysOrg : listSysOrg){
            TableTree tableTree = new TableTree();
            tableTree.setId(String.valueOf(sysOrg.getId()));
            tableTree.setName(sysOrg.getName());
            tableTree.setParentId(String.valueOf(sysOrg.getParentId()));
            list.add(tableTree);
        }
        TableTreeBuilder lb = new TableTreeBuilder(list);
        list = lb.builTree();
        return list;
    }
}
